<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/26 0026
 * Time: 16:23
 */

namespace app\admin\controller;
use think\Db;

class About extends Bases
{

    public function index(){
        $data = Db::name('connect')->where('sign="pc"')->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function  addconnect(){
        $id = intval(input('param.id'));
        $data=[
            'title'=>input('param.title'),
            'is_show'=>input('param.is_show'),
            'seo_title'=>input('param.seo_title'),
            'seo_keywords'=>input('param.seo_keywords'),
            'seo_description'=>input('param.seo_description'),
            'content'=>input('param.content'),
            'sign'=>"pc"
        ];
        if($id){
            $data['update_time']=time();
            $result = Db::name('connect')->where('id',$id)->update($data);
        }else{
            $data['create_time']=time();
            $result = Db::name('connect')->insert($data);
        }
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    //品牌中心
        public function brand(){
           $data = Db::name('brand')->where('sign="pc"')->find();
           $this->assign('data',$data);

        return $this->fetch();
        }
        public function  addbrand(){
            $id = intval(input('param.id'));
            $data=[
                'title'=>input('param.title'),
                'out_link'=>input('param.out_link'),
                'is_show'=>input('param.is_show'),
                'seo_title'=>input('param.seo_title'),
                'seo_keywords'=>input('param.seo_keywords'),
                'seo_description'=>input('param.seo_description'),
                'content'=>input('param.content'),
                'content_mobile'=>input('param.content_mobile'),
                'sign'=>"pc"
            ];
            if($id){
                $result = Db::name('brand')->where('id',$id)->update($data);
            }else{
                $data['create_time']=time();
                $result = Db::name('brand')->insert($data);
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }

        public function vcr(){
            $data = Db::name('vcr')->where('is_del=0 and is_show=1 and sign="pc"')->order('sort desc,id desc')->select();
            $this->assign('data',$data);
            return $this->fetch();
        }

        public function add_vcr(){
            $id = intval(input('param.id'));
            if(request()->isAjax()){

                $id = intval(input('param.id'));
                $data=[
                    'title'=>input('param.title'),
                    'is_show'=>intval(input('param.is_show')),
                    'sort'=>intval(input('param.sort')),
                    'click'=>intval(input('param.click')),
                    'out_link'=>input('param.out_link'),
                    'seo_title'=>input('param.seo_title'),
                    'seo_keywords'=>input('param.seo_keywords'),
                    'seo_description'=>input('param.seo_description'),
                    'sign'=>"pc"

                ];
                $main_img_file = request()->file('video_url');
                if($main_img_file){
                    $main_img_file_info =$main_img_file->move('./uploads/vcr');
                    if($main_img_file_info){
                        $data['video_url']= '/uploads/vcr/'.$main_img_file_info->getSaveName();
                    }
                }

                $video_img_file = request()->file('video_img');
                if($video_img_file){
                    $video_img_file_info =$video_img_file->move('./uploads/vcr');
                    if($video_img_file_info){
                        $data['video_img'] = '/uploads/vcr/'.$video_img_file_info->getSaveName();
                    }

                }
                if($id){
                    $old_imgs = Db::name('vcr')->where('id',$id)->field('video_img,video_url')->find();
                        if($video_img_file){
                            if($old_imgs['video_img'] !==' '){
                                unlink('.'.str_replace('\\','/',$old_imgs['video_img']));
                            }
                        }
                        if($main_img_file){
                            if($old_imgs['video_url'] !==' '){

                                unlink('.'.str_replace('\\','/',$old_imgs['video_url']));
                            }
                        }
                    $data['update_time']=time();
                    $result = DB::name('vcr')->where('id',$id)->update($data);


                }else{
                    $data['create_time']=time();

                        $result = Db::name('vcr')->insert($data);


                }
                if($result !==false){
                    return json(['code'=>1,'msg'=>'操作成功']);
                }else{
                    return json(['code'=>0,'msg'=>'操作失败']);
                }

            }
            if($id){
                $data = Db::name('vcr')->where('id',$id)->find();
                $this->assign('data',$data);
            }

            return $this->fetch();
        }


        public  function vcrdel(){
            $id = intval(input('param.id'));
            if($id){
                $result = Db::name('vcr')->where('id',$id)->delete();
            }
            if($result !==false){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }

        }


        public function delvideo(){
            $id = intval(input('param.id'));
            if($id){

                $video_url = Db::name('vcr')->where('id',$id)->value('video_url');
                if($video_url){
                    unlink('.'.str_replace('\\','/',$video_url));

                    $res = Db::name('vcr')->where('id',$id)->update(['video_url'=>' ']);
                }
                if($res){
                    return json(['code'=>1,'msg'=>'操作成功']);
                }else{
                    return json(['code'=>0,'msg'=>'操作失败']);
                }

            }
        }

    //改变排序
    public function ChangeSort(){
        $sort = (int)input('post.sort');
        $id = (int)input('post.id');
        if($id){
            $data['sort']=$sort;
            $result = Db::name('vcr')->where('id',$id)->update($data);
            if($result){
                return json(['code'=>1,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据异常']);
        }

    }





}